Repository navigation
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 408c2ebea4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const server = idle && idle.length ? idle.pop()! : null; | ||
| if (server) { serverPoolSize--; serverReused++; return { server, key }; } |
There was a problem hiding this comment.
Refresh permission snapshots before reusing an MCP server
When a member's agent_access changes from all to granted, subsequent requests retain the same pool key and reuse handlers with the old permissions. registerTools() in server/src/tools.ts snapshots restrictedNets at construction, and resolveReadScope() uses that snapshot to apply agent visibility restrictions. I reproduced this through real HTTP requests against a temporary database: after restricting a member with no agent grants, get_all_status still returned the ungranted session, with pool counters confirming reuse. This persists beyond the response-cache TTL because the permission snapshot itself is stale. Recompute mutable authorization state per request or invalidate pooled instances when permissions change.
Useful? React with 👍 / 👎.
| const server = idle && idle.length ? idle.pop()! : null; | ||
| if (server) { serverPoolSize--; serverReused++; return { server, key }; } |
There was a problem hiding this comment.
Remove pool entries when their last idle server is acquired
When requests from different caller contexts overlap near the pool limit, popping the last server leaves an empty array in serverPool. Another context can then fill the available slot before the original request finishes, causing releaseServer() to discard the original server while retaining its empty map entry indefinitely. Consequently, the 64-server limit does not bound the map or its serialized identity keys as contexts churn. Exercising the actual pool implementation with this ordering produced 10,064 map entries, including 10,000 empty entries, while serverPoolSize remained 64. Delete a context's map entry when its idle array becomes empty so long-running hubs do not accumulate this bookkeeping without limit.
Useful? React with 👍 / 👎.
背景(线上实测,只读排查)
生产 Hub(bun 1.4.4,runtime .295)主线程基本吃满一个核(87–102%),每 3–5 秒一次
[commhub stall],/health里 sessions 455 / sse 116,stall 窗口served90–190 req ⇒ 约 28 req/s。GET /health本机 curl 0.19 / 3.13 / 3.19 / 0.28 / 4.47 秒 ⇒ 不是某一条慢,是整机饱和。根因(本地 455 会话线上库副本复现)
每条
POST /mcp都new McpServer()并重新注册 105 个工具(zod → JSON Schema)。实测:ping(完全不跑工具)tools/call不存在的工具tools/call get_inboxtools/call report_statustools/call get_all_status(alias=1)GET /api/session-requests成本与「跑哪个工具」无关(ping 也要 30 ms)⇒ 全部花在重复注册工具上。28 req/s × 30 ms ≈ 840 ms/s ≈ 84% 一个核,与线上 CPU 曲线吻合。
改动
route-timing.ts:新增 tick 窗口累计(每请求 次数 / 总耗时 / 单次最大;键=路由形状,MCP 带工具名;封顶 256 键折(other))。event-loop-lag.ts:每个 tick 抽干该窗口(只清空,不排序),真停顿那次才排序取前 5;stall 日志追加| top: <route> ×N <total>ms max <max>ms; …;/health的recent_stalls[].top_routes同步带上。未接归因源时行为与旧版一致。server.ts:按调用方上下文(clientIP / 网络 / userId / callerAlias / 是否节点令牌 / tokenId / 是否要协议工具 / 身份被拒)复用已注册的McpServer;池上限 64;并发拿不到空闲实例就现建;close()成功才回池,失败丢弃。测试
mcp-server-pool-http.test.ts(4 条,真 HTTP):同上下文复用 / 换身份不复用 / 出错后复用仍能服务 / 10 并发不打死池子。route-timing.test.ts2 条 +event-loop-lag.test.ts5 条)。mcp-inflight.test.ts的源码断言跟上新的 close 结算形状(仍是 close 完成后结算、仍幂等)。未做 / 需要拍板
get_all_status缓存命中率仅 0.8%(TTL 10s vs 调用间隔 60s);GET /api/nodes889KB、/api/status737KB 的大载荷成本。